package com.zxw.flow.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zxw.common.core.base.enums.EnumBeanNameEnum;
import com.zxw.common.web.annotation.InitInsertInfo;
import com.zxw.common.web.annotation.InitSelectInfo;
import com.zxw.common.web.annotation.InitUpdateInfo;
import com.zxw.common.web.util.BeanUtils;
import com.zxw.common.web.util.JwtUtils;
import com.zxw.flow.domain.entity.FlowType;
import com.zxw.flow.domain.vo.FlowTypePageVO;
import com.zxw.flow.domain.vo.FlowTypeVO;
import com.zxw.flow.mapper.FlowTypeMapper;
import com.zxw.flow.service.FlowTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Xuewei.Zhang
 * @since 2022-07-04
 */
@Service
@RequiredArgsConstructor
public class FlowTypeServiceImpl extends ServiceImpl<FlowTypeMapper, FlowType> implements FlowTypeService {

    private final FlowTypeMapper flowTypeMapper;


    @Override
    @InitSelectInfo
    public IPage<FlowType> getPage(FlowTypePageVO pageVO) {
        Page<FlowType> flowTypeDTOPage = new Page<>(pageVO.getPageNum(), pageVO.getPageSize());
        LambdaUpdateWrapper wrapper = new LambdaUpdateWrapper<FlowType>()
                .eq(FlowType::getCompanyId, pageVO.getCompanyId())
                .eq(null != pageVO.getFlowTypeName(), FlowType::getFlowTypeName, pageVO.getFlowTypeName())
                .eq(null != pageVO.getServiceType(), FlowType::getServiceType, pageVO.getServiceType());
        Page<FlowType> pageData = flowTypeMapper.selectPage(flowTypeDTOPage, wrapper);
        return pageData;
    }

    @Override
    @InitInsertInfo
    public FlowTypeVO add(FlowTypeVO flowTypeVO) {
        flowTypeVO.setServiceType(BeanUtils.getCodeByMsg(flowTypeVO.getServiceTypeName(), EnumBeanNameEnum.FlWO));
        flowTypeMapper.insert(flowTypeVO);
        return flowTypeVO;
    }

    @Override
    @InitSelectInfo
    public FlowType getFlowTypeById(Long id) {
        Long companyId = JwtUtils.getCompanyId();
        FlowType flowType = flowTypeMapper.selectOne(new LambdaQueryWrapper<FlowType>()
                .eq(FlowType::getId, id)
                .eq(FlowType::getCompanyId, companyId)
        );
        return flowType;
    }

    @Override
    @InitUpdateInfo
    public void updateBase(FlowTypeVO flowTypeVO) {
        flowTypeMapper.update(flowTypeVO, new LambdaUpdateWrapper<FlowType>()
                .eq(FlowType::getId, flowTypeVO.getId())
                .eq(FlowType::getCompanyId, flowTypeVO.getCompanyId())
        );
    }

    @Override
    public void delete(String flowTypeIds) {
        Long companyId = JwtUtils.getCompanyId();
        flowTypeMapper.delete(new LambdaUpdateWrapper<FlowType>()
                .eq(FlowType::getCompanyId, companyId)
                .in(FlowType::getId, flowTypeIds)
        );
    }
}
